import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import concurrency.DoBackground;
import fileUtils.SendFile;

public class MainFrame {
	private JFrame mainFrame;
	private JLabel statusLabel;
	private JLabel msglabel;
	private JTabbedPane tabbedPane;
	private JTabbedPane bodyTabbedPane;
	private JPanel favoritePanel;
	private JPanel historyPanel;
	private JPanel recyclePanel;
	private JPanel configPanel;
	private JPanel aboutPanel;

	public MainFrame() {
		prepareGUI();
	}

	private void prepareGUI() {
		mainFrame = new JFrame("wangxin test tool suits");
		mainFrame.setSize(800, 600);
		mainFrame.setLayout(new BorderLayout());

		mainFrame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent windowEvent) {
				System.exit(0);
			}
		});
		statusLabel = new JLabel("NYTalk 内部测试", JLabel.CENTER);
		msglabel = new JLabel("FastDFS 并发文件测试工具", JLabel.CENTER);
		tabbedPane = new JTabbedPane();

		mainFrame.add(tabbedPane, BorderLayout.CENTER);
		mainFrame.add(msglabel, BorderLayout.NORTH);
		mainFrame.add(statusLabel, BorderLayout.SOUTH);

	}

	private void buildHome() {
		ImageIcon icon = null;
		JPanel homePanel = new JPanel();
		// homePanel.setLayout(new GridLayout(0,2));
		homePanel.setLayout(new FlowLayout());

		JLabel label1 = new JLabel();
		label1.setText("Tracker IP：");
		JTextField trackerIP = new JTextField("192.186.43.49", 10);
		trackerIP.setEnabled(false);
		JLabel label2 = new JLabel();
		label2.setText("Tracker Port:：");
		JTextField trackerPort = new JTextField("22122", 10);
		trackerPort.setEnabled(false);

		JLabel label3 = new JLabel();
		label3.setText("Storage IP：");
		JTextField storageIP = new JTextField("192.186.43.50", 10);
		storageIP.setEnabled(false);
		JLabel label4 = new JLabel();
		label4.setText("Storage Port:：");
		JTextField storagePort = new JTextField("32000", 5);
		storagePort.setEnabled(false);

		JLabel label5 = new JLabel();
		label5.setText("线程数量:：");
		JTextField threadNums = new JTextField("1", 5);

		JLabel label6 = new JLabel();
		label6.setText("并发数量:：");
		JTextField concurrenNums = new JTextField("1", 5);

		JLabel label7 = new JLabel();
		label7.setText("文件目录:：");
		final JTextField fileFolder = new JTextField("C:\\logs2", 10);

		JLabel label11 = new JLabel();
		label11.setText("API地址:：");
		final JTextField fileAPIurl = new JTextField("http://localhost:8070/webUploader/UploadServlet", 10);

		JLabel label8 = new JLabel();
		label8.setText("发送的文件:：");
		JTextField fileName = new JTextField("C:\\temp\\", 20);
		fileName.setEnabled(false);

		JLabel label9 = new JLabel();
		JButton btnSendFile = new JButton();
		btnSendFile.setText("发送");

		final JTextArea text1 = new JTextArea("log:", 25, 25);
		JScrollPane js=new JScrollPane(text1);
		//分别设置水平和垂直滚动条自动出现
		js.setHorizontalScrollBarPolicy(
		JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		js.setVerticalScrollBarPolicy(
		JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		
		btnSendFile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				SendFile.swingworkCallback(text1, 
						fileAPIurl.getText(), 
						fileFolder.getText());
			}
		});

		// homePanel.add(label1);
		// homePanel.add(trackerIP);
		// homePanel.add(label2);
		// homePanel.add(trackerPort);
		//
		// homePanel.add(label3);
		// homePanel.add(storageIP);
		// homePanel.add(label4);
		// homePanel.add(storagePort);

		homePanel.add(label5);
		homePanel.add(threadNums);

		homePanel.add(label6);
		homePanel.add(concurrenNums);

		homePanel.add(label7);
		homePanel.add(fileFolder);

		// homePanel.add(label8);
		// homePanel.add(fileName);

		homePanel.add(label11);
		homePanel.add(fileAPIurl);

		homePanel.add(btnSendFile);
		homePanel.add(label9);

		homePanel.add(text1);
		homePanel.add(js);

		tabbedPane.addTab(" Home ", icon, homePanel, "Home");
		tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
	}

	private void buildConcurrency() {
		ImageIcon icon = null;
		favoritePanel = new JPanel();

		JLabel label9 = new JLabel();
		JButton btnConcurrency = new JButton();
		btnConcurrency.setText("多线程计算阶乘(组塞主线程)");
		btnConcurrency.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				DoBackground.blockFutureGet();
			}
		});

		JButton btnNoConcurrency = new JButton();
		btnNoConcurrency.setText("计算阶乘(用主线程组塞处理)");
		btnNoConcurrency.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DoBackground.blockMainThread();
			}

		});

		JButton btnFutureTask = new JButton();
		btnFutureTask.setText("计算阶乘(FutureTask,组塞处理)");
		btnFutureTask.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DoBackground.blockFutureTaskGet();
			}

		});

		favoritePanel.add(label9);
		favoritePanel.add(btnConcurrency);
		favoritePanel.add(btnFutureTask);
		favoritePanel.add(btnNoConcurrency);

		JButton btnNoBlock1 = new JButton();
		btnNoBlock1.setText("多线程计算阶乘(用swingwork分发线程处理，无卡顿)");
		btnNoBlock1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DoBackground.swingworkMainThread();
			}

		});

		favoritePanel.add(btnNoBlock1);

		JButton btnNoBlock2 = new JButton();
		btnNoBlock2.setText("计算阶乘(用swingwork分发线程处理，无卡顿)");
		btnNoBlock2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DoBackground.swingworkMainThread();
			}

		});

		favoritePanel.add(btnNoBlock2);

		final JButton btnCallback1 = new JButton();
		btnCallback1.setText("计算阶乘(用swingwork分发线程处理回调)");
		btnCallback1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DoBackground.swingworkCallback(btnCallback1);
			}

		});

		favoritePanel.add(btnCallback1);

		tabbedPane.addTab(" Concurrency ", icon, favoritePanel, "多线程");
		// tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
	}

//	private void buildHistory() {
//		ImageIcon icon = null;
//		historyPanel = new JPanel();
//		tabbedPane.addTab(" History ", icon, historyPanel, "History");
//		// tabbedPane.setMnemonicAt(2, KeyEvent.VK_3);
//	}
//
//	private void buildRecycle() {
//		ImageIcon icon = null;
//		recyclePanel = new JPanel();
//		tabbedPane.addTab(" Recycle ", icon, recyclePanel, "Recycle");
//		// tabbedPane.setMnemonicAt(3, KeyEvent.VK_4);
//	}

	private void buildConfig() {
		ImageIcon icon = null;
		configPanel = new JPanel();
		tabbedPane.addTab(" Config ", icon, configPanel, "Config");
		// tabbedPane.setMnemonicAt(4, KeyEvent.VK_5);
	}

	private void buildAbout() {
		ImageIcon icon = null;
		aboutPanel = new JPanel();
		tabbedPane.addTab(" About ", icon, aboutPanel, "About");
		// tabbedPane.setMnemonicAt(5, KeyEvent.VK_6);
	}

	public void showJFrame() {

		buildHome();

		buildConcurrency();
		// buildHistory();
		// buildRecycle();
		buildConfig();
		buildAbout();
		mainFrame.setVisible(true);
	}

	public static void main(String[] args) {

		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				MainFrame mainUI = new MainFrame();
				mainUI.showJFrame();
			}
		});

	}
}